package com.jn.mvvm2.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jn.mvvm2.entity.News;
import com.jn.mvvm2.repository.NewsRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class NewsViewModel extends ViewModel {
    private NewsRepository mNewsRepository;
    private MutableLiveData<News> mNewsLiveData = new MutableLiveData<>();

    public NewsViewModel() {
        mNewsRepository = new NewsRepository();
    }

    public void getData() {
        mNewsRepository.getData().subscribe(new Observer<News>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d("asdf", "onSubscribe");
            }

            @Override
            public void onNext(@NonNull News news) {
                Log.d("asdf", "onNext: " + news.toString());
                mNewsLiveData.postValue(news);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d("asdf", "onError");
            }

            @Override
            public void onComplete() {
                Log.d("asdf", "onComplete");
            }
        });
    }

    public LiveData<News> getLiveData() {
        return mNewsLiveData;
    }
}
